﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;

namespace csharp窗口句柄z
{
    //添加新建类Win32Api，该类封装Api函数，代码如下：

    public class Win32Api
    {

        [StructLayout(LayoutKind.Sequential)]

        public class POINT
        {
            public int x;

            public int y;

        }

        [StructLayout(LayoutKind.Sequential)]

        public class MouseHookStruct
        {

            public POINT pt;

            public int hwnd;

            public int wHitTestCode;

            public int dwExtraInfo;

        }
        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄

        public static extern int WindowFromPoint(int xPoint, int yPoint);

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

        //安装钩子

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        //卸载钩子

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern bool UnhookWindowsHookEx(int idHook);

        //调用下一个钩子

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

    }
}
